//
//  HomeHotCell.m
//  ImGuider
//
//  Created by 李陆涛 on 2017/12/26.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "HomeHotCell.h"
#import "LineNodeCCell.h"
#import "DestinationNodeCCell.h"
#import "SpecialNodeCCell.h"
#import "ScenicNodeCCell.h"
#import "TicketNodeCCell.h"

#import "LinePreDetailVC.h"
#import "CityVC.h"
#import "ScenicDescribeVC.h"
#import "SpecialDetailVC.h"
#import "SpecialListVC.h"
#import "TickectDetailVC.h"
@interface HomeHotCell () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionViewH;

@end

@implementation HomeHotCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (void)setType:(HomeCellType)type {
    
    _type = type;
    
    switch (type) {
        case HomeCellTypeLine: {
            
            self.collectionViewH.constant = (kScreenWidth * 320 / 375) * 9 / 16 + 72;
            [self.collectionView registerNib:[UINib nibWithNibName:@"LineNodeCCell" bundle:nil] forCellWithReuseIdentifier:@"LineNodeCCell"];
        }
            break;
        case HomeCellTypeDestination: {
            
            self.collectionViewH.constant = (kScreenWidth * 140 / 375) * 200 / 145;
            [self.collectionView registerNib:[UINib nibWithNibName:@"DestinationNodeCCell" bundle:nil] forCellWithReuseIdentifier:@"DestinationNodeCCell"];
        }
            break;
        case HomeCellTypeSpecial: {
            
            self.collectionViewH.constant = (kScreenWidth * 320 / 375) * 9 / 16;
            [self.collectionView registerNib:[UINib nibWithNibName:@"SpecialNodeCCell" bundle:nil] forCellWithReuseIdentifier:@"SpecialNodeCCell"];
        }
            break;
        case HomeCellTypeScenic: {
            
            self.collectionViewH.constant = (kScreenWidth * 140 / 375);
            
            [self.collectionView registerNib:[UINib nibWithNibName:@"ScenicNodeCCell" bundle:nil] forCellWithReuseIdentifier:@"ScenicNodeCCell"];
        }
            break;
            
        case HomeCellTypeTiket: {
            
            self.collectionViewH.constant = (kScreenWidth * 320 / 375) * 9 / 16;
            
            [self.collectionView registerNib:[UINib nibWithNibName:@"TicketNodeCCell" bundle:nil] forCellWithReuseIdentifier:@"TicketNodeCCell"];
        }
            break;
            
        default:
            break;
    }
}

- (void)setDataSource:(NSArray *)dataSource {
    
    _dataSource = dataSource;
    [self.collectionView reloadData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    switch (self.type) {
        case HomeCellTypeLine: {
            
            LineModel *model = self.dataSource[indexPath.row];
            
            LineNodeCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LineNodeCCell" forIndexPath:indexPath];
            if (!model.country) {
                cell.isCountry = YES;
            }
            cell.model = model;
            return cell;
        }
            break;
        case HomeCellTypeDestination: {
            
            DestinationNodeCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DestinationNodeCCell" forIndexPath:indexPath];
            cell.model = self.dataSource[indexPath.row];
            return cell;
        }
            break;
        case HomeCellTypeSpecial: {
            
            LineModel *model = self.dataSource[indexPath.row];
            
            SpecialNodeCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SpecialNodeCCell" forIndexPath:indexPath];
            cell.model = model;
            return cell;
        }
            break;
        case HomeCellTypeScenic: {
            
            ScenicNodeCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ScenicNodeCCell" forIndexPath:indexPath];
            
            [cell configDataWith:self.dataSource[indexPath.row]];
            
            return cell;
        }
            break;
        case HomeCellTypeTiket: {
            
            TicketModel *ticket = self.dataSource[indexPath.row];
            
            TicketNodeCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TicketNodeCCell" forIndexPath:indexPath];
            
            cell.ticket = ticket;
            
            return cell;
        }
            break;
            
        default:
            break;
    }
    
    
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat width = 0;
    CGFloat height = 0;
    
    
    switch (self.type) {
        case HomeCellTypeLine: {
            
            width = kScreenWidth * 320 / 375;
            height = width * 9 / 16 + 72;
        }
            break;
        case HomeCellTypeDestination: {
            
            width = kScreenWidth * 140 / 375;
            height = width * 200 / 145;
        }
            break;
        case HomeCellTypeSpecial: {
            
            width = kScreenWidth * 320 / 375;
            height = width * 9 / 16;
        }
            break;
        case HomeCellTypeScenic: {
            
            width = kScreenWidth * 140 / 375;
            height = width;
        }
            break;
            
        case HomeCellTypeTiket: {
            
            width = kScreenWidth * 320 / 375;
            height = width * 9 / 16;
        }
            
        default:
            break;
    }
    
    return CGSizeMake(width, height);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (self.type) {
            case HomeCellTypeLine: {
                
                LineModel *model = self.dataSource[indexPath.row];
                
                LinePreDetailVC *detailVC = [IGUtil loadViewController:@"LinePreDetailVC" storyboard:@"LineDetail"];
                detailVC.lineid = model.lineid;
                detailVC.hidesBottomBarWhenPushed = YES;
                
                [self.superVC.navigationController pushViewController:detailVC animated:YES];
            }
            break;
            case HomeCellTypeDestination: {

                City *city = self.dataSource[indexPath.row];
                
                CityVC *cityVC = [CityVC new];
                cityVC.cid = city.cid.stringValue;
                
                cityVC.hidesBottomBarWhenPushed = YES;
                [self.superVC.navigationController pushViewController:cityVC animated:YES];
            }
            break;
            case HomeCellTypeSpecial: {
                LineModel *model = self.dataSource[indexPath.row];
                SpecialDetailVC * specialDetailVC = [[SpecialDetailVC alloc] init];
                
                specialDetailVC.specialid = model.lineid;
                
                specialDetailVC.hidesBottomBarWhenPushed = YES;
                [self.superVC.navigationController pushViewController:specialDetailVC animated:YES];
             
            }
            break;
            case HomeCellTypeScenic: {
                
                ScenicDescribeVC *scenicDesVC = [ScenicDescribeVC new];
                
                scenicDesVC.hidesBottomBarWhenPushed = YES;
                Scenic *scenic = self.dataSource[indexPath.row];
                scenicDesVC.scenicid = scenic.ID;
                [self.superVC.navigationController pushViewController:scenicDesVC animated:YES];
            }
            break;
            
        case HomeCellTypeTiket: {
            
            TicketModel * ticket = self.dataSource[indexPath.row];
            TickectDetailVC * vc = [[TickectDetailVC alloc] init];
            vc.ticketID = ticket.tid;
            vc.hidesBottomBarWhenPushed = YES;
            [self.superVC.navigationController pushViewController:vc animated:YES];
        }
            break;
            
        default:
            break;
    }
}

@end

